part of base.temples;

class TabState<W extends StatefulWidget> extends State<W> {
  List<TabOption> tabOptions;
  int tabIndex = 0;
  final double selectedFontSize;

  final double unselectedFontSize;

  final bool? showUnselectedLabels;
  final bool? showSelectedLabels;
  final selectedItemColor;
  final unselectedItemColor;

  TabState(
      {this.selectedFontSize: 14,
      this.unselectedFontSize: 14,
      this.showUnselectedLabels: true,
      this.showSelectedLabels: true,
      this.selectedItemColor,
      this.unselectedItemColor,
      this.tabOptions = const <TabOption>[]})
      : super();

  get bottomNavItems =>
      this.tabOptions.map((child) => child.navigationBarItem).toList();

  get child => this.tabOptions.map((child) => child.children).toList();

  get _bar => BottomNavigationBar(
        items: bottomNavItems,
        // 显示选中的文字
        showSelectedLabels: showSelectedLabels,
        // 显示不选中时的问题
        showUnselectedLabels: showUnselectedLabels,
        selectedFontSize: selectedFontSize,
        unselectedFontSize: unselectedFontSize,
        // 未选中颜色
        unselectedItemColor: unselectedItemColor,
        // 选中颜色
        selectedItemColor: selectedItemColor,
        currentIndex: tabIndex,
        type: BottomNavigationBarType.fixed,
        onTap: (index) {
          setState(() {
            if (tabIndex != index) tabIndex = index;
          });
        },
      );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: IndexedStack(
        children: child,
        index: tabIndex,
      ),
      bottomNavigationBar: _bar,
    );
  }
}

class TabOption {
  TabOption(
      {required this.children,
      required this.navigationBarItem,
      required this.tabIndex});

  Widget children;
  BottomNavigationBarItem navigationBarItem;
  int tabIndex;
}
